package de.pmg_grandma;

import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class SimpleGestureFilter extends SimpleOnGestureListener {
	public final static int SWIPE_UP    = 1;
	public final static int SWIPE_DOWN  = 2;

	private boolean running      = true;
	private boolean tapIndicator = false;

	private Activity context;
	private GestureDetector detector;
	private SimpleGestureListener listener;

	public SimpleGestureFilter(Activity context,SimpleGestureListener sgl) {
		this.detector = new GestureDetector(context, this);
		this.listener = sgl;
	}

	public void onTouchEvent(MotionEvent event){

		if(!this.running)
			return;

		boolean result = this.detector.onTouchEvent(event);

	}

	public void setEnabled(boolean status){
		this.running = status;
	}


	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		
		
		float yKoordinate_e1 = e1.getY();
		float yKoordinate_e2 = e2.getY();
		
		boolean result = false;
		
		if(Math.abs(e2.getY()) > Math.abs(e1.getY())){
			this.listener.onSwipe(SWIPE_DOWN);
			result= true;
		}
		else if(Math.abs(e2.getY()) < Math.abs(e1.getY())){
			this.listener.onSwipe(SWIPE_UP);
			result= true;
		}
		return result;
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		this.tapIndicator = true;
		return false;
	}


	@Override
	public boolean onDoubleTapEvent(MotionEvent arg) {
		return true;
	}


	static interface SimpleGestureListener{
		void onSwipe(int direction);
		void onDoubleTap();
	}


	public SimpleGestureListener getListener() {
		return listener;
	}

	public void setListener(SimpleGestureListener listener) {
		this.listener = listener;
	}

}
